PhoneExt = new Object();

PhoneExt.load = function() {
	
	//active bt on ihm
	$('#photoGrp').css('display','block');
	$('#phoneGrp').css('display','block');
	
	//active fonction
	$('#photo_link').live('click', PhoneExt.photo);
	$('#image_link').live('click', PhoneExt.image);
	Context.addContact = PhoneExt.addContact;
};

PhoneExt.onFail = function(message) {
	console.log(message);
};

PhoneExt.addContact = function(firstname, lastname) {
	
	try {
		var contact = navigator.service.contacts.create();
		
		contact.displayName = firstname + " " + lastname;
		contact.nickname = firstname + " " + lastname;     //specify both to support all devices
		
		var name = new ContactName();
		name.givenName = firstname;
		name.familyName = lastname;
		contact.name = name;
	
		contact.save(function () { },
	            function (err) {PhoneExt.onFail("error in add contact : " + err.code); });
	} catch(exception) {
		PhoneExt.onFail(exception);
  	}   
	
};

PhoneExt.onPhotoURISuccess = function(imageURI) {
	$('#image').attr('src', imageURI).css('display','block');
};

PhoneExt.onPhotoDataSuccess = function(imageData) {
    $('#image').attr('src', "data:image/jpeg;base64," + imageData).css('display','block');
};

PhoneExt.photo = function(event){
		
	try {
		navigator.camera.getPicture(PhoneExt.onPhotoDataSuccess, PhoneExt.onFail, { quality: 50 } );
	} catch(exception) {
		PhoneExt.onFail(exception);
	}
};
    
PhoneExt.image = function(event){
		
	try {
        
        var pictureSource=navigator.camera.PictureSourceType;
        var destinationType=navigator.camera.DestinationType;
		
		navigator.camera.getPicture(PhoneExt.onPhotoURISuccess, PhoneExt.onFail, 
									{quality: 50, destinationType: destinationType.FILE_URI, sourceType: pictureSource.PHOTOLIBRARY} );
    } catch(exception) {
    	PhoneExt.onFail(exception);
  	}   	
};

PhoneExt.uploadImage = function (imageURI, url) {
	
    var options = new FileUploadOptions();
    options.fileKey="file";
    options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
    options.mimeType="image/jpeg";

    var params = new Object();
    params.value1 = "test";
    params.value2 = "param";

    options.params = params;

    var ft = new FileTransfer();
    ft.upload(imageURI, url, win, fail, options);
};

